
Partial Class Admin_UserAdmin
    Inherits BasePage
    Private allusers As MembershipUserCollection = Membership.GetAllUsers
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim Letters() As String = "A;B;C;D;E;F;G;H;I;J;K;L;M;N;O;P;Q;R;S;T;U;V;W;X;Y;Z;All".Split(";")
            LetterList.DataSource = Letters
            LetterList.DataBind()

        Else
        End If
        lblTotalUsers.Text = allusers.Count
        lblUsersOnline.Text = Membership.GetNumberOfUsersOnline

    End Sub

    Protected Sub LetterList_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.RepeaterCommandEventArgs) Handles LetterList.ItemCommand
        gvUsers.Attributes.Add("SearchByEmail", False.ToString)
        If e.CommandArgument.ToString.Length = 1 Then
            gvUsers.Attributes.Add("SearchText", e.CommandArgument.ToString & "%")
        Else
            gvUsers.Attributes.Add("SearchText", "")
        End If
        bindusers(False)
    End Sub
    Private Sub BindUsers(ByVal ReloadAllUsers As Boolean)
        Dim SearchText As String = ""
        Dim users As MembershipUserCollection
        Dim SearchByEmail As Boolean
        If Not String.IsNullOrEmpty(gvUsers.Attributes("SearchText")) Then
            SearchText = gvUsers.Attributes("SearchText")
            SearchByEmail = False
        Else
            If Not String.IsNullOrEmpty(gvUsers.Attributes("SearchByEmail")) Then
                SearchByEmail = Boolean.Parse(gvUsers.Attributes("SearchByEmail"))
            End If
        End If
        If SearchText.Length > 0 Then
            If SearchByEmail Then
                users = Membership.FindUsersByEmail(SearchText)
            Else
                users = Membership.FindUsersByName(SearchText)
            End If
        Else
            users = allusers
        End If
        gvUsers.DataSource = users
        gvUsers.DataBind()
    End Sub

    Protected Sub gvUsers_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvUsers.RowCreated
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim btn As ImageButton
            btn = e.Row.Cells(6).Controls(0)
            btn.OnClientClick = "if (confirm('Are you sure you want to delete this user account?')== false) return false;"
        End If
    End Sub

    Protected Sub gvUsers_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles gvUsers.RowDeleting
        Dim username As String
        username = gvUsers.DataKeys(e.RowIndex).Value.ToString
        ProfileManager.DeleteProfile(username)
        Membership.DeleteUser(username)
        BindUsers(True)
        Me.lblTotalUsers.Text = allusers.Count
    End Sub

    Protected Sub btnFind_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnFind.Click
        If txtFind.Text.Length > 0 Then
            gvUsers.Attributes.Add("FindByEmail", True.ToString)
            gvUsers.Attributes.Add("SearchText", txtFind.Text)
            BindUsers(False)
        End If
    End Sub

End Class
